package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Pet;
import service.PetService;


public class AddServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String name =request.getParameter("name");
		int breed =Integer.parseInt(request.getParameter("breed"));
		int sex =Integer.parseInt(request.getParameter("sex"));
		String birthday =request.getParameter("birthday");
		String description =request.getParameter("description");
		PetService service=new PetService();
		Pet pet=new Pet();
		pet.setName(name);
		pet.setBreed(breed);
		pet.setSex(sex);
		pet.setBirthday(birthday);
		pet.setDescription(description);
		int result=service.add(pet);
		PrintWriter out=response.getWriter();
		if (result>0) {
			out.print("<script>alert('新增成功!');location.href='LIstServlet'</script>");
		} else {
			out.print("<script>alert('新增失败!');location.back()</script>");
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
